package com.chat.http.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chat.constant.tab.CertificateConstant;
import com.chat.http.service.PlanConnectionService;
import com.chat.http.validator.OnlinePeopleVoValidator;
import com.chat.vo.params.OnlinePeopleVo;
import com.chat.websocket.online.OnlineManage;

/**
 * 用户准备连接
 * 
 * @author lwh
 *
 */
@Controller
@RequestMapping("/")
public class PlanConnectionController {

	@Autowired
	private OnlinePeopleVoValidator thisValidator;

	@Autowired
	private PlanConnectionService planConnectionService;

	@RequestMapping("/planConn")
	@ResponseBody
	public Map<String, String> planConn(String accesskey, String secretkey, OnlinePeopleVo people,
			BindingResult result) {
		Map<String, String> map = new HashMap<String, String>(0);
		thisValidator.planConnValidate(accesskey, secretkey, people, result);
		if (result.hasErrors()) {
			map.put("status", "error");
			map.put("massage", result.getAllErrors().get(0).getCode());
			return map;
		} else {
			// 校对凭证信息
			boolean flag = planConnectionService.valiatorCertificate(CertificateConstant.TYPE_ADMIN.getValue(),
					accesskey, secretkey);
			if (flag == true) {
				OnlineManage.addToPlanConnTemp(people.getUserName(), people.getToken());
				map.put("status", "success");
				return map;
			}
			map.put("status", "error");
			map.put("massage", "凭证错误");
			return map;
		}
	}

}
